﻿
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// 对 List<Point> 的一些扩展方法
/// </summary>
public static class ListPointHelper
{
   /// <summary>
   /// 列表是否存在指定点
   /// </summary>
   /// <param name="points"></param>
   /// <param name="point"></param>
   /// <returns></returns>
   public static bool Exists(this List<Point> points, Point point)
   {
      if (points == null || point == null)
         return false;
      int count = points.Count;
      for (int i = 0; i < count; i++)
      {
         var p = points[i];
         if ((p.X == point.X) && (p.Y == point.Y))
            return true;
      }
      return false;
   }
   /// <summary>
   ///  列表是否存在指定点
   /// </summary>
   /// <param name="points"></param>
   /// <param name="x"></param>
   /// <param name="y"></param>
   /// <returns></returns>
   public static bool Exists(this List<Point> points, int x, int y)
   {
      if (points == null)
         return false;
      int count = points.Count;
      for (int i = 0; i < count; i++)
      {
         var p = points[i];
         if (p.X == x && p.Y == y)
            return true;
      }
      return false;
   }

   /// <summary>
   /// 获取列表中最小的点
   /// </summary>
   /// <param name="points"></param>
   /// <returns></returns>
   public static Point MinPoint(this List<Point> points)
   {
      points = points.OrderBy(p => p.F).ToList();
      return points[0];
   }

   /// <summary>
   /// 添加点
   /// </summary>
   /// <param name="points"></param>
   /// <param name="x"></param>
   /// <param name="y"></param>
   public static void Add(this List<Point> points, int x, int y)
   {
      Point point = new Point(x,y);
      points.Add(point);
   }

   /// <summary>
   /// 获取点
   /// </summary>
   /// <param name="points"></param>
   /// <param name="point"></param>
   /// <returns></returns>
   public static Point Get(this List<Point> points, Point point)
   {
      if (points == null || point == null)
         return null;
      int count = points.Count;
      for (int i = 0; i < count; i++)
      {
         var p = points[i];
         if (p.X == point.X && p.Y == point.Y)
            return p;
      }
      return null;
   }

   /// <summary>
   /// 移除点
   /// </summary>
   /// <param name="points"></param>
   /// <param name="x"></param>
   /// <param name="y"></param>
   public static void Remove(this List<Point> points, int x, int y)
   {
      if(points == null)
         return;
      int count = points.Count;
      for (int i = 0; i < count; i++)
      {
         var p = points[i];
         if (p.X == x && p.Y == y)
            points.Remove(p);
      }
   }
}